hyper-tungstenite
This crate allows hyper
servers to accept websocket connections, backed by tungstenite
.
The upgrade
function allows you to upgrade a HTTP connection to a websocket connection.
It returns a HTTP response to send to the client, and a future that resolves to a WebSocketStream
.
The response must be sent to the client for the future to be resolved.
In practise this means that you must spawn the future in a different task.
Note that the upgrade
function itself does not check if the request is actually an upgrade request.
For simple cases, you can check this using the is_upgrade_request
function before calling upgrade
.
For more complicated cases where the server should support multiple upgrade protocols,
you can manually inspect the Connection
and Upgrade
headers.
Example
use ;
use ;
use ;
use Message;
/// Handle a HTTP or WebSocket request.
async
/// Handle a websocket connection.
async